<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="formInline.user" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="formInline.phone" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="学生状态">
        <el-select v-model="formInline.status" placeholder="选择状态">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
        <el-button type="primary" @click="scxz">删除选中</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="reslist"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="nickname" label="学生名称" width="200">
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" width="180">
      </el-table-column>
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">{{
          scope.row.status ? "启用" : "禁用"
        }}</template>
      </el-table-column>
      <el-table-column label="创建时间" width="180">
        <template slot-scope="scope">{{
          scope.row.created_at | flitime
        }}</template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" @click="remove(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="list.length"
      :page-size="pageSize"
      :current-page="pageNum"
      @current-change="changePagenum"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      copylist: [],
      formInline: {
        user: "",
        phone: "",
        status: "",
      },
      pageSize: 5,
      pageNum: 1,
      removeIds: "",
    };
  },
  methods: {
    // 查询
    onSubmit() {
      if (this.formInline.status == "") {
        var res = this.copylist.filter((item) => {
          return (
            item.nickname.includes(this.formInline.user) &&
            item.mobile.includes(this.formInline.phone)
          );
        });
      } else {
        var res = this.copylist.filter((item) => {
          return (
            item.nickname.includes(this.formInline.user) &&
            item.mobile.includes(this.formInline.phone) &&
            item.status == this.formInline.status
          );
        });
      }
      this.list = res;
    },
    // 重置
    reset() {
      this.formInline.user = "";
      this.formInline.status = "";
      this.formInline.phone = "";
      this.list = this.copylist;
    },
    // 选中
    handleSelectionChange(val) {
      var res = val.map((item) => {
        return item.uid;
      });
      this.removeIds = res.join(",");
      console.log(this.removeIds);
      // console.log(res);
    },
    // 删除选中
    scxz() {
      this.list = this.list.filter((item) => {
        if (this.removeIds.indexOf(item.uid) == -1) {
          return item;
        }
      });
    },
    // 编辑
    edit() {},
    // 删除
    remove(index) {
      this.list.splice(index, 1);
    },
    //
    changePagenum(val) {
      this.pageNum = val;
    },
  },
  mounted() {
    this.$axios.get("/list.json").then((res) => {
      console.log(res);
      this.list = res.data;
      this.copylist = res.data;
    });
  },
  filters: {
    flitime(val) {
      return new Date(val * 1000).toLocaleString();
    },
  },
  computed: {
    reslist() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
};
</script>

<style>
</style>